﻿using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LeHouse.Server.WebApi.Common
{
    public class GetFinalAuditNumber
    {
        /// <summary>
        /// 得到一个汉字的拼音第一个字母，如果是一个英文字母则直接返回大写字母
        /// </summary>
        /// <param name="CnChar">单个汉字</param>
        /// <returns>单个大写字母</returns>
        public static string GetCharSpellCode(string T_ReportNumber)
        {
            Collateral_PersonalService bll = new Collateral_PersonalService();
            var getnumber = GETnumber(T_ReportNumber);
            if (bll.Select(p => p.T_FinalAuditNumber == getnumber) != null)
            {
                return GetCharSpellCode(T_ReportNumber);
            }
            else
            {
                return getnumber;
            }
        }
        public static string GETnumber(string T_ReportNumber)
        {
            System_DepartmentService Department = new System_DepartmentService();//部门
            Collateral_PersonalService Personal = new Collateral_PersonalService();//抵押物评估登记基本信息表
            Collateral_Personal Per = Personal.Select(p => p.T_ReportNumber == T_ReportNumber).FirstOrDefault();
            System_Department GetPersonalname = Department.Select(p => p.T_DepartmentID == Per.T_DepartmentID).FirstOrDefault();
            var CnChar = GetPersonalname.T_Dname;
            Random n3 = new Random();
            var num1 = GetPersonalname.T_DCode;
            var num3 = n3.Next(10000, 99999);
            var num1number = num1;
            var num3number = num3.ToString("00000");
            long iCnChar;
            byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar);
            //如果是字母，则直接返回
            if (ZW.Length == 1)
            {
                return CnChar.ToUpper();
            }
            else
            {
                int i1 = (short)(ZW[0]);
                int i2 = (short)(ZW[1]);
                iCnChar = i1 * 256 + i2;
            }
            if ((iCnChar >= 45217) && (iCnChar <= 45252))
            {
                return DateTime.Now.Year + "-" + "A" + num1number + "-" + num3number;
            }
            if ((iCnChar >= 45253) && (iCnChar <= 45760))
            {
                return DateTime.Now.Year + "-" + "B" + num1number + "-" + num3number;
            }
            if ((iCnChar >= 45761) && (iCnChar <= 46317))
            {
                return DateTime.Now.Year + "-" + "C" + num1number + "-" + num3number;
            }
            if ((iCnChar >= 46318) && (iCnChar <= 46825))
            {
                return DateTime.Now.Year + "-" + "D" + num1number + "-" + num3number;
            }
            if ((iCnChar >= 46826) && (iCnChar <= 47009))
            {

                return DateTime.Now.Year + "-" + "E" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 47010) && (iCnChar <= 47296))
            {

                return DateTime.Now.Year + "-" + "F" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 47297) && (iCnChar <= 47613))
            {

                return DateTime.Now.Year + "-" + "G" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 47614) && (iCnChar <= 48118))
            {

                return DateTime.Now.Year + "-" + "H" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 48119) && (iCnChar <= 49061))
            {

                return DateTime.Now.Year + "-" + "J" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 49062) && (iCnChar <= 49323))
            {

                return DateTime.Now.Year + "-" + "K" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 49324) && (iCnChar <= 49895))
            {

                return DateTime.Now.Year + "-" + "L" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 49896) && (iCnChar <= 50370))
            {

                return DateTime.Now.Year + "-" + "M" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 50371) && (iCnChar <= 50613))
            {

                return DateTime.Now.Year + "-" + "N" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 50614) && (iCnChar <= 50621))
            {

                return DateTime.Now.Year + "-" + "O" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 50622) && (iCnChar <= 50905))
            {

                return DateTime.Now.Year + "-" + "P" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 50906) && (iCnChar <= 51386))
            {

                return DateTime.Now.Year + "-" + "Q" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 51387) && (iCnChar <= 51445))
            {

                return DateTime.Now.Year + "-" + "R" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 51446) && (iCnChar <= 52217))
            {

                return DateTime.Now.Year + "-" + "S" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 52218) && (iCnChar <= 52697))
            {

                return DateTime.Now.Year + "-" + "T" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 52698) && (iCnChar <= 52979))
            {

                return DateTime.Now.Year + "-" + "W" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 52980) && (iCnChar <= 53640))
            {

                return DateTime.Now.Year + "-" + "X" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 53689) && (iCnChar <= 54480))
            {

                return DateTime.Now.Year + "-" + "Y" + num1number + "-" + num3number;

            }
            if ((iCnChar >= 54481) && (iCnChar <= 55289))
            {

                return DateTime.Now.Year + "-" + "Z" + num1number + "-" + num3number;

            }
            else
            {

                return ("?");
            }
        }
    }
}